import * as validateUtils from './validate'

/**
 * 时间戳转为标准时间
 * @param {Object} timeStamp
 */
export function timeStampTodateTime(timeStamp) { //timeStamp：毫秒格式，即13位   如果是秒就 *1000
	let s = new Date(timeStamp)
	let y = s.getFullYear()
	let m = (s.getMonth() + 1) < 10 ? '0' + (s.getMonth() + 1) : (s.getMonth() + 1)
	let dd = s.getDate() < 10 ? '0' + s.getDate() : s.getDate()
	let hh = s.getHours() < 10 ? '0' + s.getHours() : s.getHours()
	let mm = s.getMinutes() < 10 ? '0' + s.getMinutes() : s.getMinutes()
	let ss = s.getSeconds() < 10 ? '0' + s.getSeconds() : s.getSeconds()
	let enddate = y + '-' + m + '-' + dd + ' ' + hh + ':' + mm + ":" + ss
	return enddate
}

/**
 * hex 转 rgba 格式
 */
export function hexToRgba(hex, opacity = 1) {
	return {
		r: parseInt("0x" + hex.slice(1, 3)),
		g: parseInt("0x" + hex.slice(3, 5)),
		b: parseInt("0x" + hex.slice(5, 7)),
		a: opacity
	}
}

/**
 * 匹配body之间的内容
 * @param {Object} content
 */
export function getBody(content) {
	const REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/;
	const result = REG_BODY.exec(content);
	if (result && result.length === 2)
		return result[1];
	return content;
}

/**
 * 判断是否为空 校验
 * @param {Object} value
 */
export function isEmpty(value) {
	let result = false;
	if (value == null || value == undefined) {
		result = true;
	}
	if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
		result = true;
	}
	// 判断空数组
	if (typeof value == "object" && (value instanceof Array || Array.isArray(value) || value.constructor === Array) &&
		value.length === 0) {
		result = true;
	}
	return result;
}

/**
 * 获取 num 天前日期
 * @param {*} num 
 * @returns 
 */
export function getDate(num) {
	var date1 = new Date();
	var date2 = new Date(date1);
	date2.setDate(date1.getDate() + num);
	//num是正数表示之后的时间，num负数表示之前的时间，0表示今天
	var time2 = addZero(date2.getFullYear()) + "-" + addZero((date2.getMonth() + 1)) + "-" + addZero(date2.getDate());
	return time2;
}

function addZero(num) { //补0方法
	if (parseInt(num) < 10) {
		num = '0' + num;
	}
	return num;
}

// 获取token值
export function getToken(tokenKey = 'token') {
	return uni.getStorageSync(tokenKey);
}

// input校验
export function isValidInput({ required, requiredMessage, dataTypeCheck, dataType, dataTypeMessage, patternCheck, pattern, patternMessage, value }) {
	let validator = false
	let validateMessage = ''
	if (required) {
		if (!value) {
			validateMessage = requiredMessage
			validator = true
			return { validator, validateMessage }
		}
	}
	
	if (dataTypeCheck) {
		if (!isNext(dataType, value)) {
			validateMessage = dataTypeMessage
			validator = true
			return { validator, validateMessage }
		}
	}
	
	if (patternCheck) {
		if (!new RegExp(pattern)?.test(value)) {
			validateMessage = patternMessage
			validator = true
			return { validator, validateMessage }
		}
	}
	
	return { validator, validateMessage }
}

// 单行文本dataType校验
function isNext(type, value) {
	const map = {
		string: () => true,
		number: validateUtils.isDigits,
		integer: validateUtils.isNumber,
		float: validateUtils.isFloat,
		url: validateUtils.isURL,
		email: validateUtils.isEmail,
		hex: validateUtils.isHexString
	}
	return map[type](value) || false
}
